package com.mustafaiev.tair.mweather;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mustafaiev.tair.mweather.exception.MWeatherParseException;
import com.mustafaiev.tair.mweather.forecast.Forecast;
import com.mustafaiev.tair.mweather.forecast.Town;
import com.mustafaiev.tair.mweather.xml.MWeatherXMLParser;

/**
 * Creation date: 2 Sep 2011 01:59:10
 * 
 * @author Tair Mustafaiev
 * 
 */
public class ForecastListViewActivity extends ListActivity {

	private MWeatherForecastListAdapter mAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.main_activity_layout);

		Town town = new Town();
		town.setTownId(33345);
		MWeatherXMLParser parser = new MWeatherXMLParser();
		ArrayList<Forecast> forecasts = null;
		try {
			forecasts = parser.getParsedForecast(town.getTownId());
		} catch (MWeatherParseException e) {
			Log.e("ForecastListViewProvider, method onCreate()", e.toString());
		}
		this.mAdapter = new MWeatherForecastListAdapter();

		for (Forecast forecast : forecasts) {
			this.mAdapter.addItem(forecast);
		}

		this.setListAdapter(this.mAdapter);
	}

	/**
	 * Creation date: 2 Sep 2011 01:59:19
	 * 
	 * @author Tair Mustafaiev
	 * 
	 */
	private class MWeatherForecastListAdapter extends BaseAdapter {

		private final ArrayList<Forecast> mData = new ArrayList<Forecast>();
		private final LayoutInflater mInflater;

		public MWeatherForecastListAdapter() {
			this.mInflater = (LayoutInflater) ForecastListViewActivity.this
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public void addItem(Forecast item) {
			this.mData.add(item);
			this.notifyDataSetChanged();
		}

		public int getCount() {
			return this.mData.size();
		}

		public Object getItem(int position) {
			return this.mData.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder holder = null;
			if (convertView == null) {
				convertView = this.mInflater.inflate(
						R.layout.forecast_item_view, null);
				holder = new ViewHolder();
				holder.textView = (TextView) convertView
						.findViewById(R.id.itemTemp);
				convertView.setTag(holder);

			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			String maxTemp = Integer.toString(this.mData.get(position)
					.getMaxTemperature());
			String minTemp = Integer.toString(this.mData.get(position)
					.getMinTemperature());
			String time = Integer.toString(this.mData.get(position).getHour());
			holder.textView.setText(minTemp + "..." + maxTemp + "\u00B0C  "
					+ " for " + time + "h");
			return convertView;
		}

	}

	/**
	 * Creation date: 2 Sep 2011 01:30:19
	 * 
	 * @author Tair Mustafaiev
	 * 
	 */
	public static class ViewHolder {
		public TextView textView;
		public RelativeLayout relativeLayout;
	}

}
